
import { hex_md5 } from "../utils/Md5";
import BasePlatform from "./BasePlatform";
import PlatformManager from "./PlatformManager";
import { getNetParams, getQueryVariable, loadjsScript } from "../GlobalFunction";
import { NetManager } from "../Managers/NetManager";
import GameEvent from "../Commons/GameEvent";

export default class QunHeiPlatform extends BasePlatform {

    initSdk() {
        if (CC_BUILD) {

            this.query = window.location.search.substring(1);

        } else if (CC_DEBUG) {

            //132 ，733312221162.783855014964594
            let username = cc.sys.localStorage.getItem("userName");
            if (!username) {
                username = "7333122211" + Math.random() * 100;
            }
            cc.sys.localStorage.setItem("userName", username);
            this.query = "username=" + username + "&qhchannel=qunhei_1635405318_7154417_0_2_30_1_18817f5d3062281e4e92badb24836007_90b3ae82b637be39fad3db236531899c_1" + "&serverid=1&time=1342123&isadult=1&nname=张晓珊&unid=test123&flag=test1&showbtn=1&usertag=qunhei&uimg=xxxx";
        }
        // let fp = window["Fingerprint2"]();
        // let that = this;
        // fp.get((result, components) => {
        //     that.ecid = result;

        // });
        let url = "//m.heitu.com/game/qhjssdk";
        loadjsScript(url, this.loadScriptComplete);
    }

    loadScriptComplete(): any {
        let tsdk = window["qhsdk"];
        PlatformManager.single.basePlatform.sdk = tsdk;
        let data = {
            "username": getQueryVariable('username', PlatformManager.single.basePlatform.query), //用户id，群黑登录接口里面username参数
            "gid": '5176', //群黑游戏id，可以在后台游戏列表查询
            "qhchannel": getQueryVariable('qhchannel', PlatformManager.single.basePlatform.query), //用户标识，群黑登录接口里面qhchannel参数
            "qhchannelid": getQueryVariable('qhchannelid', PlatformManager.single.basePlatform.query), //用户标识id，群黑登录接口里面qhchannelid参数
            "time": getQueryVariable('time', PlatformManager.single.basePlatform.query), //用户登录时间戳，群黑登录接口里面time参数
            "serverid": PlatformManager.single.basePlatform.serverid,
            "isadult": getQueryVariable('isadult', PlatformManager.single.basePlatform.query),
            "unid": getQueryVariable('unid', PlatformManager.single.basePlatform.query),
            "flag": getQueryVariable('flag', PlatformManager.single.basePlatform.query),
            "showbtn": getQueryVariable('showbtn', PlatformManager.single.basePlatform.query),
            "usertag": getQueryVariable('usertag', PlatformManager.single.basePlatform.query),
            "uimg": getQueryVariable('uimg', PlatformManager.single.basePlatform.query),
            "loginsys": getQueryVariable('loginsys', PlatformManager.single.basePlatform.query)
        };
        PlatformManager.single.basePlatform.openId = data["username"];
        PlatformManager.single.basePlatform.platForm = "qunhei";
        PlatformManager.single.basePlatform.qhchannel = data["qhchannel"];
        console.log("渠道qhchannel：", PlatformManager.single.basePlatform.qhchannel);
        if (CC_BUILD) {
            PlatformManager.single.basePlatform.sdk.init(data);
        }
        GameEvent.eventNode.emit(GameEvent.LOADSDKCOMPLETE_EVENT);

        /**
         * 群黑 登录
         */
        // PlatformManager.single.basePlatform.login();

    }
    onShareMessage(e) {
        e = e || event;
        if (e.data == 'shareok') {
            //分享成功
            // alert('分享成功');
        }
    }


    createAd(obj: any) {

    }

    role(data: any) {
        // let key: string = "55367df58762aea38ea274e55f455620";
        // let power: number = -1;
        // let level: number = -1;
        // let str: string = data.act + "_" + this.openId + "_" + PlayerManager.single.playerInfo.serverId + "_" +
        //     PlayerManager.single.playerInfo.roleId + "_" + level + "_" + power + "_" + "-1" + "_" + key;
        // // console.log("signstr：" + str);
        // let sign: string = hex_md5(str);
        // // console.log("sign：" + sign);
        // var roledata = {
        //     "act": data.act,
        //     "username": this.openId,
        //     "serverid": PlayerManager.single.playerInfo.serverId,
        //     "rolename": PlayerManager.single.playerInfo.nickName,
        //     "servername": PlayerManager.single.playerInfo.serverId,
        //     "roleid": PlayerManager.single.playerInfo.roleId,
        //     "level": level,
        //     "power": power,
        //     "vip": -1,
        //     "rolecreatetime": GameHelp.nowTime,
        //     "sign": sign
        // }
        // this.sdk.role(roledata);
    }

    pay(goodsId: number) {
        let param = getNetParams({ goodsId: goodsId }, true);
        let promise: Promise<void> = new Promise((resolve, reject) => {
            // NetManager.Instance.doPost(`/pay/payOrder`, param, (code: number, response: any) => {
            //     if (code != 0) {
            //         reject(response);
            //         return;
            //     }
            //     this.sdk.pay(response.data);
            //     resolve();
            // });
        })
        return promise;
    }

    checkWord(wordstr: string, cb: Function) {
        this.sdk.checkWord(wordstr, (res) => {// 10000 没有敏感词
            // 10001 有敏感词
            // 10010 未知异常(如网络问题)
            if (res.code == "10001") {
                // TipsHelp.single.showTxt("存在敏感词，请重新输入");
            } else if (res.code == "10010") {
                // TipsHelp.single.showTxt("未知异常");
            } else if (res.code == "10000") {
                if (cb != null) {
                    cb();
                }
            }
        });
    }
    /**刷新地址 */
    reloadUrl() {
        if (this.sdk != null) {
            this.sdk.reloadUrl();
        }
    }


}